package com.woniu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.CheckDto;
import com.woniu.dto.CheckListDto;
import com.woniu.dto.PageDto;
import com.woniu.dto.QueryCheckDto;
import com.woniu.exception.CheckException;
import com.woniu.model.HisCheck;
import com.woniu.mapper.HisCheckMapper;
import com.woniu.param.CreateOrModifyCheckParam;
import com.woniu.param.DelCheckParam;
import com.woniu.param.PageParam;
import com.woniu.param.QueryCheckParam;
import com.woniu.service.IHisCheckService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 检查项目表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-11
 */
@Service
public class HisCheckServiceImpl extends ServiceImpl<HisCheckMapper, HisCheck> implements IHisCheckService {

    @Override
    public CheckListDto createOrModifyCheck(CreateOrModifyCheckParam param) {
        if (ObjectUtil.isNull(param.getCheckParamList()) || param.getCheckParamList().size() == 0) {
            throw new CheckException(1017, "无参数");
        }

        List<HisCheck> checkList = CopyBeanUtil.copyBean(param.getCheckParamList(), HisCheck.class);
        if (!this.saveOrUpdateBatch(checkList)) {
            throw new CheckException(1018, "保存或修改失败");
        }

        CheckListDto dto = new CheckListDto();
        List<CheckDto> checkDtoList = CopyBeanUtil.copyBean(checkList, CheckDto.class);
        dto.setCheckDtoList(checkDtoList);

        return dto;
    }

    @Override
    public void delCheck(DelCheckParam param) {
        if (ObjectUtil.isNull(param.getCheckIdList()) || param.getCheckIdList().size() == 0) {
            throw new CheckException(1017, "无参数");
        }

        if (!this.removeByIds(param.getCheckIdList())) {
            throw new CheckException(1018, "删除失败");
        }
    }

    @Override
    public QueryCheckDto queryCheck(QueryCheckParam param) {

        QueryWrapper<HisCheck> wrapper = new QueryWrapper<>();

        if(ObjectUtil.isNull(param.getQueryCriteriaCheckParam())) {
            throw new CheckException(1036, "查询条件不能为空");
        }

        //查询条件
        if (ObjectUtil.isNotNull(param.getQueryCriteriaCheckParam().getCaseId())) {
            wrapper.eq("case_id", param.getQueryCriteriaCheckParam().getCaseId());
        }
        if (ObjectUtil.isNotNull(param.getQueryCriteriaCheckParam().getCheckId())) {
            wrapper.eq("check_id", param.getQueryCriteriaCheckParam().getCheckId());
        }
        if (ObjectUtil.isNotNull(param.getQueryCriteriaCheckParam().getPatientId())) {
            wrapper.eq("patient_id", param.getQueryCriteriaCheckParam().getPatientId());
        }

        //不需要分页
        if (BeanUtil.isEmpty(param.getPageParam())) {
            param.setPageParam(new PageParam(1L, 1000L));
        }
        Page<HisCheck> page = this.page(new Page<>(param.getPageParam().getPage(),
                param.getPageParam().getPageSize()), wrapper);

        QueryCheckDto dto = new QueryCheckDto();
        dto.setCheckDtoList(PageDto.bulid(page, CheckDto.class));

        return dto;
    }

    //缴费成功，修改状态
    @Override
    public void changeCheck(Integer patientId) {
        List<HisCheck> checks = this.list(new QueryWrapper<HisCheck>().eq("patient_id", patientId).eq("check_ispay", 0));
        for (HisCheck check : checks) {
            check.setCheckIspay(1);
        }

        if (!this.updateBatchById(checks)) {
            throw new CheckException(7777, "检查单缴费状态修改失败");
        }
    }
}
